# vim:filetype=make
# make.rules
# Written by Jonas Juselius <jonas@iki.fi> Tue Dec  4 11:41:04 EET 2001
#

ifeq ($(srcdir),)
	srcdir:=.
endif

all_src:=$(shell find $(srcdir) -iname "*.f90" -o -iname "*.f" -o -iname "*.c" -o -iname "*.cxx")
all_objs:=$(addsuffix .o, $(basename $(subst $(topdir),$(builddir),$(all_src))))
tag_src:=$(filter-out lex.yy.c, $(all_src))
tag_src:=$(filter-out y.tab.c, $(tag_src))

src:=$(all_src)
objs:=$(all_objs)
# search path used by make to find dependencies
VPATH=$(includedir):$(libdir):$(bindir)

.PHONY: all
all:

# TAGS and tags are for people working with vi(m) or emacs who wants to
# use the tag feature of the editors (VERY useful...)
#.PHONY: TAGS tags
TAGS: $(tag_src)
	$(ETAGS) $(tag_src)

tags: $(tag_src)
	$(CTAGS) $(tag_src)

.PHONY: archive
archive:
	git-archive --format=tar --prefix=$(ARCHIVE_NAME)/ HEAD | gzip -c >\
		$(ARCHIVE_TARNAME).tgz

.PHONY: clean realclean distclean
clean:
	-@set -e; \
	for type in \*.o \*.mod \*.d \*.pyc \*.pyo \*.gdca \*.gcno; do \
	   echo "find . -name "$$type" -delete"; \
	   find . -name "$$type" -delete; \
	done \
#	echo "rm -f *.o *.pyc *.pyo"; \
#	rm -f *.o *.mod *.d *.pyc *.pyo;
ifneq ($(FC_MODEXT),)
	-@set -e; \
	echo "rm -f *.$(FC_MODEXT)"; \
	rm -f *.$(FC_MODEXT);
endif
ifneq ($(INST_PROGS),)
	@set -e; for i in $(INST_PROGS); do \
	echo "rm -f $$i"; rm -f $$i; done
endif
ifneq ($(INST_SCRIPTS),)
	@set -e; for i in $(INST_SCRIPTS); do \
	echo "rm -f $$i"; rm -f $$i; done
endif
ifneq ($(INST_LIBS),)
	@set -e; for i in $(INST_LIBS); do \
	echo "rm -f $$i"; rm -f $$i; done
endif

realclean: clean
	-rm -f tags TAGS y.tab.c y.tab.h lex.yy.c deps.mk;

distclean: realclean
	-rm -f Makefile make.config make.rules config.h
	-rm -f config.cache config.log config.status
	-rm -rf autom4te.cache

.PHONY: install install_bin install_lib install_modules  install_data
.PHONY: install_libexec install_scripts

inst_targets:=
ifneq ($(INST_LIBS),)
	INST_LIBS:=$(addprefix $(libdir)/,$(INST_LIBS))
	inst_targets+=install_lib
endif
ifneq ($(INST_LIBEXEC),)
	INST_LIBEXEC:=$(addprefix $(libexecdir)/,$(INST_LIBEXEC))
	inst_targets+=install_libexec
endif
ifneq ($(INST_PROGS),)
	INST_PROGS:=$(addprefix $(bindir)/,$(INST_PROGS))
	inst_targets+=install_bin
endif
ifneq ($(INST_INCLUDES),)
	INST_INCLUDES:=$(addprefix $(includedir)/,$(INST_INCLUDES))
	inst_targets+=install_includes
endif
ifneq ($(INST_MODULES),)
	INST_MODULES:=$(addprefix $(includedir)/,$(INST_MODULES))
	inst_targets+=install_modules
endif
ifneq ($(INST_DATA),)
	INST_DATA:=$(addprefix $(datadir)/,$(INST_DATA))
	inst_targets+=install_data
endif
ifneq ($(INST_SCRIPTS),)
	INST_SCRIPTS:=$(addprefix $(scriptdir)/,$(INST_SCRIPTS))
	inst_targets+=install_scripts
endif

install: $(inst_targets)

install_bin: $(INST_PROGS) $(inst_bindir)
ifneq ($(INST_PROGS),)
	@set -e; \
	for i in $(INST_PROGS); do \
		$(INSTALL_PROGRAM) $$i $(inst_bindir); \
		echo "Installed $$i in $(inst_bindir)"; \
	done
endif

install_lib: $(INST_LIBS) $(inst_libdir)
ifneq ($(INST_LIBS),)
	@set -e; \
	for i in $(INST_LIBS); do \
		$(INSTALL_DATA) $$i $(inst_libdir); \
		echo "Installed $$i in $(inst_libdir)"; \
	done
endif

install_libexec: $(INST_LIBEXEC) $(inst_libexecdir)
ifneq ($(INST_LIBEXEC),)
	@set -e; \
	for i in $(INST_LIBEXEC); do \
		$(INSTALL_DATA) $$i $(inst_libexecdir); \
		echo "Installed $$i in $(inst_libexecdir)"; \
	done
endif

install_data: $(INST_DATA) $(inst_datadir)
ifneq ($(INST_DATA),)
	@set -e; \
	for i in $(INST_DATA); do \
		$(INSTALL_DATA) $$i $(inst_datadir); \
		echo "Installed $$i in $(inst_datadir)"; \
	done
endif

install_scripts: $(INST_SCRIPTS) $(inst_bindir)
ifneq ($(INST_SCRIPTS),)
	@set -e; \
	for i in $(INST_SCRIPTS); do \
		$(INSTALL_PROGRAM) $$i $(inst_bindir); \
		echo "Installed $$i in $(inst_bindir)"; \
	done
endif

install_includes: $(INST_INCLUDES) $(inst_includedir)
ifneq ($(INST_INCLUDES),)
	@set -e; \
	for i in $(INST_INCLUDES); do \
		  $(INSTALL_DATA) $$i $(inst_includedir); \
		echo "Installed $$i in $(inst_includedir)"; \
	done
endif

ifeq ($(FC_MODCASE), uppercase)
    modname=$(addsuffix .$(FC_MODEXT),$(shell echo $(1) |tr '[:lower:]' '[:upper:]'))
else
    modname=$(addsuffix .$(FC_MODEXT),$(shell echo $(1) |tr '[:upper:]' '[:lower:]' ))
endif

ifneq ($(INST_MODULES),)
  INST_MODULES:=$(call modname, $(INST_MODULES))
endif

install_modules: $(inst_includedir)
ifneq ($(INST_MODULES),)
	@set -e; \
	for i in $(INST_MODULES); do \
	  $(INSTALL_DATA) $$i $(inst_includedir); \
	  echo "Installed $$i in $(inst_includedir)"; \
	done
endif

$(inst_bindir):
	@$(mkinstalldirs) $(inst_bindir)

$(inst_libdir):
	@$(mkinstalldirs) $(inst_libdir)

$(inst_libexecdir):
	@$(mkinstalldirs) $(inst_libexecdir)

$(inst_datadir):
	@$(mkinstalldirs) $(inst_datadir)

$(inst_includedir):
	@$(mkinstalldirs) $(inst_includedir)

%.o: %.c
	$(CC) $(CFLAGS) -c $<

%.o: %.cxx
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -o $@ -c $<

%.o: %.f
	$(FC) $(FCFLAGS) $(GPUFLAGS) -c $<

%.o: %.F
	$(FC) $(FCFLAGS) $(GPUFLAGS) -c $<

%.o: %.f90
	$(FC) $(FCFLAGS) $(GPUFLAGS) -c $<

(%.o): %.o
	$(AR) rc $@ $<

$(includedir)/%.$(FC_MODEXT): %.$(FC_MODEXT)
	cp -f $< $@

